package com.ruoyi.kenuo.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.kenuo.domain.KernelMyPatient;
import com.ruoyi.kenuo.mapper.KernelMyPatientMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.kenuo.mapper.KernelMeasureRecordMapper;
import com.ruoyi.kenuo.domain.KernelMeasureRecord;
import com.ruoyi.kenuo.service.IKernelMeasureRecordService;
import com.ruoyi.common.core.domain.AjaxResult;
/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author polo tang
 * @date 2023-09-08
 */
@Service
public class KernelMeasureRecordServiceImpl implements IKernelMeasureRecordService
{
    @Autowired
    private KernelMeasureRecordMapper kernelMeasureRecordMapper;
    @Autowired
    private KernelMyPatientMapper kernelMyPatientMapper;



    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public KernelMeasureRecord selectKernelMeasureRecordById(Long id)
    {
        return kernelMeasureRecordMapper.selectKernelMeasureRecordById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param kernelMeasureRecord 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<KernelMeasureRecord> selectKernelMeasureRecordList(KernelMeasureRecord kernelMeasureRecord)
    {
        return kernelMeasureRecordMapper.selectKernelMeasureRecordList(kernelMeasureRecord);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param kernelMeasureRecord 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertKernelMeasureRecord(KernelMeasureRecord kernelMeasureRecord)
    {
       // kernelMeasureRecord.setCreateTime(DateUtils.getNowDate());
        String mu = kernelMeasureRecord.getMeasureUser();
        String re = kernelMeasureRecord.getRemarkThree();
        if(StringUtils.isNotEmpty(mu)&&kernelMeasureRecord.getRemarkThree().equals("1000")){
            //去查询我的好友医生列表
            KernelMyPatient kmt = new KernelMyPatient();
            kmt.setClientUname(mu);
            List<KernelMyPatient> kl = kernelMyPatientMapper.selectKernelMyPatientList(kmt);
            if(kl.size()>0){
                kernelMeasureRecord.setDoctorUser(kl.get(0).getDoctorUname());
                kernelMeasureRecord.setDoctorUserId(Long.valueOf(kl.get(0).getRemarkOne()));
                kernelMeasureRecord.setDoctorUserName(kl.get(0).getDockerKname());
            }
        }
        kernelMeasureRecord.setCreateTime(DateUtils.getNowDate());
        return kernelMeasureRecordMapper.insertKernelMeasureRecord(kernelMeasureRecord);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param kernelMeasureRecord 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateKernelMeasureRecord(KernelMeasureRecord kernelMeasureRecord)
    {
        kernelMeasureRecord.setUpdateTime(DateUtils.getNowDate());
        return kernelMeasureRecordMapper.updateKernelMeasureRecord(kernelMeasureRecord);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteKernelMeasureRecordByIds(Long[] ids)
    {
        return kernelMeasureRecordMapper.deleteKernelMeasureRecordByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteKernelMeasureRecordById(Long id)
    {
        return kernelMeasureRecordMapper.deleteKernelMeasureRecordById(id);
    }

    @Override
    public AjaxResult getRecentMeasure(KernelMeasureRecord kernelMeasureRecord) {
        String measureUser = kernelMeasureRecord.getMeasureUser();
        if(StrUtil.isEmpty(measureUser)){
            return AjaxResult.error("measureUser is null");
        }
        //先查询左眼的数据
        kernelMeasureRecord.setLeftOr(1L);
        List<KernelMeasureRecord> li = kernelMeasureRecordMapper.selectKernelMeasureRecordList(kernelMeasureRecord);
        Map<String,Object> mp = new HashMap<>();
        if(!li.isEmpty()){
            mp.put("leftValue",li.get(0).getMeasureValue());
            mp.put("leftState",li.get(0).getState());
            mp.put("leftMeasureTime",DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,li.get(0).getMeasureTime()));
        }
        kernelMeasureRecord.setLeftOr(2L);
        List<KernelMeasureRecord> ri = kernelMeasureRecordMapper.selectKernelMeasureRecordList(kernelMeasureRecord);
        if(!ri.isEmpty()){
            mp.put("rightValue",ri.get(0).getMeasureValue());
            mp.put("rightState",ri.get(0).getState());
            mp.put("rightMeasureTime",DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,ri.get(0).getMeasureTime()));
        }

        return AjaxResult.success(mp);
    }

    @Override
    public List<KernelMeasureRecord> getAbnormalList(KernelMeasureRecord kernelMeasureRecord) {
        List<KernelMeasureRecord> list =  kernelMeasureRecordMapper.getAbnormalList(kernelMeasureRecord);
        return list;
    }

    @Override
    public AjaxResult getStatisticsList(KernelMeasureRecord kernelMeasureRecord) {
        Map<String,Object> mp = new HashMap<>();
        //查询一天之内
        List<KernelMeasureRecord> dayList =  kernelMeasureRecordMapper.getDayList(kernelMeasureRecord);
        //查询一周之内
        List<KernelMeasureRecord> weekList =  kernelMeasureRecordMapper.getWeekList(kernelMeasureRecord);
        //查询一月之内
        List<KernelMeasureRecord> monthList =  kernelMeasureRecordMapper.getMonthList(kernelMeasureRecord);
        mp.put("day",dayList);
        mp.put("week",weekList);
        mp.put("month",monthList);
        return AjaxResult.success(mp);
    }

    @Override
    public List<KernelMeasureRecord> getAbList(KernelMeasureRecord kernelMeasureRecord) {
        String doctorUser = kernelMeasureRecord.getDoctorUser();
        //根据医生的账号查询名下患者的账号
        KernelMyPatient kernelMyPatient = new KernelMyPatient();
        kernelMyPatient.setDoctorUname(doctorUser);
        List<KernelMyPatient> plist = kernelMyPatientMapper.selectKernelCurPatientList(kernelMyPatient);
        List<KernelMeasureRecord> tlist = new ArrayList<>();
        if(!plist.isEmpty()){
            plist.stream().forEach(x->{
                KernelMeasureRecord ktr = new KernelMeasureRecord();
                ktr.setMeasureUser(x.getClientUname());
                List<KernelMeasureRecord> list =  kernelMeasureRecordMapper.getAbnormalList(ktr);
                if(!list.isEmpty()){
                    tlist.addAll(list);
                }
            });
        }
        return tlist;
    }

    @Override
    public List<KernelMeasureRecord> getClientAbList(KernelMeasureRecord kernelMeasureRecord) {
        List<KernelMeasureRecord> list =  kernelMeasureRecordMapper.getClientAbList(kernelMeasureRecord);
        return list;
    }


}
